package cn.wolfcode.dao.impl;

import cn.wolfcode.dao.IStudentDAO;
import cn.wolfcode.domain.Student;
import lombok.Cleanup;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class StudentDAOImpl implements IStudentDAO {

  /*  private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }*/

    public void save(String username, String password) throws Exception {
//获取连接对象
        @Cleanup
        Connection connection = dataSource.getConnection();
        String sql="insert into student(username,password)values (?,?)";
        @Cleanup
        PreparedStatement ps = connection.prepareStatement(sql);
       //给占位符赋值
        ps.setString(1,username);
        ps.setString(2,password);
      ps.executeUpdate();
    }
private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void insert(Student student) throws Exception {
        Connection connection = dataSource.getConnection();
        PreparedStatement ps =
                connection.prepareStatement("insert into student(username,password)values (?,?)");
        ps.setString(1,student.getUsername());
        ps.setString(2,student.getPassword());
        ps.executeUpdate();
    }
}
